<?php
   class ApiCenter_SalesApi_Model_Api2_Orderapi_Rest extends ApiCenter_SalesApi_Model_Api2_Orderapi{
       const  IS_CONFIRM =2;
       
       const  NO_CONFIRM =1;
       /**
        * 筛选条件
        *   vendor_id
        *   simple
        *   status
        * @return Mage_Sales_Model_Resource_Order_Collection
        */

       protected function _getCollectionForRetrieve($vendorId)
       { 
           $status  =$this->getRequest()->getParam('status');
           
           $pageNumber  =$this->getRequest()->getParam('page');
           $pageSize  =$this->getRequest()->getParam('limit');
           Mage::log($pageNumber."-".$pageSize);
           //如果有搜索条件会查tb_sales_flat_order订单表，同时会默认筛选掉未确认的总订单
           try{
           if($status == ApiCenter_SalesApi_Model_Api2_Orderapi::IS_CONFIRM){
               $collection = Mage::getResourceModel('salesapi/order_collection');
               $collection->addFieldToFilter("main_table.vendor_id",$this->_loadVendor($vendorId)->getId());
               $collection->getSelect()
               ->joinLeft(array('d' => 'sales_flat_order'),
                    'LEFT(main_table.increment_id,9) = d.increment_id',
                    array(
                         "main_created"=>"d.created_at"
                   ));        

             $this->_applyCollectionModifiers($collection);

           }elseif ($status ==ApiCenter_SalesApi_Model_Api2_Orderapi::NO_CONFIRM){
      
               /** @var $collection ApiCenter_SalesApi_Model_Resource_Mysql4_Order_Item_Collection */
               $collection = Mage::getResourceModel('sales/order_item_collection');
               $collection->addFieldToFilter("main_table.vendor_id",$this->_loadVendor($vendorId)->getId());
                $collection->addFieldToFilter("main_table.parent_item_id",array('null' => ''));
               $collection->addFieldToFilter("d.status","payment_success");
               $collection->addFieldToSelect("sku");
               $collection->getSelect()
               ->joinLeft(array('d' => 'sales_flat_order'),
                   'd.entity_id = main_table.order_id',
                   array(
                       "increment_id"=>"d.increment_id",
                       "order_id"=>"d.entity_id",
                       "main_created_at"=>"d.created_at",
                       "main_status"=>"d.status"
                   ))
                   ->joinLeft(array('f' => 'tb_sales_flat_order'),
                       'LEFT(f.increment_id,9) = d.increment_id AND (`main_table`.`vendor_id` = `f`.`vendor_id`)',
                       array(
                           "fincrement_id"=>"f.increment_id",
                       ))
                   ->columns(
                       array('sum_price' => new Zend_Db_Expr('sum(main_table.base_row_total)')
                       ))
                       
                 ->group("main_table.order_id")     
                 ->where(" f.increment_id is null");
                
               $this->_applyCollectionModifiers($collection);
              // $collection->setCurPage($pageNumber)->setPageSize($pageSize);
                     }
             }catch(Exception $e){
                  Mage::log("api2->_getCollectionForRetrieve()");
                  Mage::log($e->getMessage());
             }
           return $collection;
       } 
       protected function _getCountForRetrieve($vendorId)
       {
       
            $status  =$this->getRequest()->getParam('status');
           //如果有搜索条件会查tb_sales_flat_order订单表，同时会默认筛选掉未确认的总订单
           try{
               if($status == ApiCenter_SalesApi_Model_Api2_Orderapi::IS_CONFIRM){
                  $collection = Mage::getResourceModel('salesapi/order_collection');
                  $collection->addFieldToFilter("main_table.vendor_id",$this->_loadVendor($vendorId)->getId());
                  $collection->getSelect()
                  
                  ->joinLeft(array('d' => 'sales_flat_order'),
                    'LEFT(main_table.increment_id,9) = d.increment_id',
                    array(
                         "main_created"=>"d.created_at"
                   ));        
                   
                   $this->_applyFilter($collection);
                  return count($collection);
               }elseif($status == ApiCenter_SalesApi_Model_Api2_Orderapi::NO_CONFIRM){
                   /** @var $collection ApiCenter_SalesApi_Model_Resource_Mysql4_Order_Item_Collection */
                   $collection = Mage::getResourceModel('sales/order_item_collection');
                   $collection->addFieldToFilter("main_table.vendor_id",$this->_loadVendor($vendorId)->getId());
                   $collection->addFieldToFilter("main_table.parent_item_id",array('null' => ''));
                   $collection->addFieldToFilter("d.status","payment_success");
                   $collection->addFieldToSelect("sku");
                   $collection->getSelect()
                   ->joinLeft(array('d' => 'sales_flat_order'),
                       'd.entity_id = main_table.order_id',
                       array(
                           "increment_id"=>"d.increment_id",
                           "main_created_at"=>"d.created_at",
                           "status"=>"d.status"
                       ))
                      ->joinLeft(array('f' => 'tb_sales_flat_order'),
                       'LEFT(f.increment_id,9) = d.increment_id AND (`main_table`.`vendor_id` = `f`.`vendor_id`)',
                       array(
                           "fincrement_id"=>"f.increment_id",
                       ))
                   ->group("main_table.order_id")    
                     ->where(" f.increment_id is null");
                   $this->_applyFilter($collection);
                  
              return count($collection);
               }
           }catch(Exception $e){
               Mage::log("api2->_getCollectionForRetrieve()");
               Mage::log($e->getMessage());
           }
         
       }
  }